# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Steven Allen <steven@stebalien.com>
# Contributor: Hugo Osvaldo Barrera <hugo@barrera.io>

pkgname=rootlesskit
pkgver=1.1.1
pkgrel=1
pkgdesc='Native "fake root" for implementing rootless containers'
arch=('loong64' 'x86_64')
url='https://github.com/rootless-containers/rootlesskit'
license=('Apache')
depends=('glibc' 'shadow')
makedepends=('git' 'go')
checkdepends=('openbsd-netcat')
options=('!lto')
_commit='a2c596ff9b3fddc0c2becb38f2ef4004f15765b5'
source=("$pkgname::git+https://github.com/rootless-containers/rootlesskit.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare(){
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS}" \
    -o build \
    ./cmd/...
}

# tests fail :(
#check() {
#  cd "$pkgname"
#  go test -v ./...
#}

package() {
  cd "$pkgname"

  # binaries
  install -vDm755 -t "$pkgdir/usr/bin" build/*

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md
  cp -vr docs "$pkgdir/usr/share/doc/$pkgname"
}
